import { ref, type Ref } from "vue";
import { createRouter, createWebHistory } from "vue-router";
import type { RouteRecordRaw, Router } from "vue-router";
import { vueMetaEnv } from "@/settings";

export const Layout = () => import("@/layout/index.vue");
export const LoginPage = () => import("@/views/login/index.vue");
export const RegisterPage = () => import("@/views/register/index.vue");
export const MultiFactorLoginPage = () =>
  import("@/views/login/components/types.vue");
export const ProfilePage = () => import("@/views/my/profile.vue");
export const Error401Page = () => import("@/views/error-page/401.vue");
export const Error404Page = () => import("@/views/error-page/404.vue");
export const DashboardPage = () => import("@/views/dashboard/index.vue");
export const RedirectPage = () => import("@/views/redirect/index.vue");

export const ChangeOrgan = () => import("@/views/change-organ/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: RedirectPage,
      },
    ],
  },
  {
    path: "/register",
    component: RegisterPage,
    meta: { hidden: true },
  },
  {
    path: "/login",
    component: LoginPage,
    meta: { hidden: true },
  },
  {
    path: "/changeOrgan",
    component: ChangeOrgan,
    meta: { hidden: true },
  },
  {
    path: "/secondLogin",
    component: MultiFactorLoginPage,
    meta: { hidden: true, title: "手机号/邮箱登录" },
  },
  {
    path: "/my",
    component: Layout,
    meta: { hidden: true },
    redirect: "noredirect",
    children: [
      {
        path: "profile",
        name: "ProfilePage",
        component: ProfilePage,
        meta: { title: "个人中心", icon: "user", keepAlive: true },
      },
    ],
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "/dashboard",
        component: DashboardPage,
        name: "Dashboard", // 用于 keep-alive, 必须与SFC自动推导或者显示声明的组件name一致
        // https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        meta: {
          title: "Dashboard",
          icon: "homepage",
          affix: true,
          keepAlive: true,
          alwaysShow: false,
        },
      },
      {
        path: "401",
        component: Error401Page,
        meta: { hidden: true },
      },
      {
        path: "404",
        component: Error404Page,
        meta: { hidden: true },
      },
    ],
  },
];

// 动态路由样例
export const testRoutes: RouteRecordRaw[] = [
  {
    path: "/test",
    component: Layout,
    meta: { roleCode: "admin" },
    children: [
      {
        path: "profile1",
        component: ProfilePage,
        name: "Profile",
        meta: {
          title: "个人中心",
          icon: "user",
          permCode: "benefits:benefits_info:list",
        },
      },
    ],
  },
];

//import { addDynamicRoute } from "jmash-core";
//addDynamicRoute(testRoutes);

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHistory(vueMetaEnv.APP_BASE_URL),
  routes: constantRoutes as RouteRecordRaw[],
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

/**
 * 创建路由
 */
const routerRef: Ref = ref(router);

/**
 * 重置路由
 */
export function resetRouter() {
  routerRef.value.replace({ path: "/login" });
}

export function updateRouter(newRouter: Router) {
  routerRef.value = newRouter;
}

export default routerRef;
